package com.woniuxy.qiantai.listener;

import com.rabbitmq.client.Channel;
import com.woniuxy.qiantai.entity.Order;
import com.woniuxy.qiantai.mapper.OrderMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class DelayCancelOrderListener {

    @Autowired
    OrderMapper orderMapper;

    @RabbitListener(queues = "cancleOrderDeadLetterQueue")
    public void onMsg(Long orderId, Channel channel, Message message) throws IOException {

        System.out.println("延迟队列的消费者对订单进行处理 orderId: "+orderId);

        //检查订单状态,在只有在订单还是未支付状态时进行取消
        Order order = orderMapper.selectById(orderId);
        if (order.getState() == 1){
            order.setState(5);
            orderMapper.updateById(order);

            System.out.println("订单尚未支付,执行取消操作");
        }else {
            System.out.println("订单已支付,无需操作");
        }

        //手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
